﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TechTalk.SpecFlow;
using YasharEl.Infrastructure.Core.ServiceModel;
using YasharEl.Infrastructure.ProcessEngine.ServiceModel;
using YasharEl.Infrastructure.ServiceModel;

namespace YasharEl.Infrastructure.AllModules.AcceptanceTests.ProcessEngine.Steps
{
    [Binding]
    public class RetrievingProcessDefinitionWithIncorrectApplicationSuppliedShouldThrowApplicationNotBelongsFaultStepDefinition
    {
        #region Fields

        private RetrieveProcessDefinitionRequest _retrieveProcessDefinitionRequest;
        private RetrieveProcessDefinitionResponse _retrieveProcessDefinitionResponse;
        private PropertyDefinitionDTO _testLongProperty;
        private ProcessDefinitionAccessDeniedFault _thrownFault;

        #endregion

        #region Given

        [Given(@"I have filled out the process definition id : (.*)")]
        [Scope(Tag = "RetrievingProcessDefinitionWithIncorrectApplicationSuppliedShouldThrowApplicationNotBelongsFault")]
        public void GivenIHaveFilledOutTheProcessDefinitionId(long processDefinitionId)
        {
            _retrieveProcessDefinitionRequest = new RetrieveProcessDefinitionRequest()
            {
                ProcessDefinitionId = processDefinitionId
            };
        }

        [Given(@"I have filled out the incorrect application id : (.*)")]
        [Scope(Tag = "RetrievingProcessDefinitionWithIncorrectApplicationSuppliedShouldThrowApplicationNotBelongsFault")]
        public void GivenIHaveFilledOutTheIncorrectApplicationId(long applicationId)
        {
            _retrieveProcessDefinitionRequest.ApplicationId = applicationId;
        }

        #endregion

        #region When

        [When(@"I try to retrieve process metadata which not belongs to given application from process definitions catalog")]
        [Scope(Tag = "RetrievingProcessDefinitionWithIncorrectApplicationSuppliedShouldThrowApplicationNotBelongsFault")]
        public void WhenITryToRetrieveProcessMetadataWhichNotBelongsToGivenApplicationFromProcessDefinitionsCatalog()
        {
            try
            {
                using (ServiceProxy<IProcessMetadataRetriever> processMetadataRetrieverProxy = new ServiceProxy<IProcessMetadataRetriever>())
                {
                    processMetadataRetrieverProxy.Proxy.RetrieveProcessDefinition(_retrieveProcessDefinitionRequest);
                }
            }
            catch (FaultException<ProcessDefinitionAccessDeniedFault> e)
            {
                _thrownFault = e.Detail;
            }
        }

        #endregion

        #region Then

        [Then(@"process definition not belongs to application fault thrown")]
        [Scope(Tag = "RetrievingProcessDefinitionWithIncorrectApplicationSuppliedShouldThrowApplicationNotBelongsFault")]
        public void ThenProcessDefinitionNotBelongsToApplicationFaultThrown()
        {
            Assert.IsNotNull(_thrownFault);
        }

        [Then(@"fault message contains the given not belongs to application definition id: (.*)")]
        [Scope(Tag = "RetrievingProcessDefinitionWithIncorrectApplicationSuppliedShouldThrowApplicationNotBelongsFault")]
        public void ThenFaultMessageContainsTheGivenNotBelongsToApplicationDefinitionId(long processDefinitionId)
        {
            Assert.AreEqual<long>(processDefinitionId, _thrownFault.RequestedProcessDefinition);
        }

        [Then(@"fault message contains the given incorrect application id: (.*)")]
        [Scope(Tag = "RetrievingProcessDefinitionWithIncorrectApplicationSuppliedShouldThrowApplicationNotBelongsFault")]
        public void ThenFaultMessageContainsTheGivenIncorrectApplicationId(long applicationId)
        {
            Assert.AreEqual<long>(applicationId, _thrownFault.RequestedApplication);
        }

        #endregion
    }
}
